#include <common/Network.h>

namespace flashpp {

Device&
Network::addDevice(std::string name) {
  nameToDevice_.emplace(
      std::piecewise_construct,
      std::forward_as_tuple(name),
      std::forward_as_tuple(name));
  return nameToDevice_.at(name);
}

void
Network::addLink(
    std::string dev1, std::string port1, std::string dev2, std::string port2) {
  if (nameToDevice_.find(dev1) == nameToDevice_.end()) {
    addDevice(dev1);
  }
  if (nameToDevice_.find(dev2) == nameToDevice_.end()) {
    addDevice(dev2);
  }
  nameToDevice_.at(dev1).addNeighbor(port1, dev2);
  nameToDevice_.at(dev2).addNeighbor(port2, dev1);

  Port& p1 = nameToDevice_.at(dev1).getCreatePort(port1);
  Port& p2 = nameToDevice_.at(dev2).getCreatePort(port2);
  p1.setPeer(&p2);
  p2.setPeer(&p1);
}

} // namespace flashpp